//
//  CourseCommentViewController.m
//  TeacherNet
//
//  Created by slz on 2017/11/27.
//  Copyright © 2017年 bnsr. All rights reserved.
//
#import <EDStarRating.h>

#define leftRightMargin 22
#define lineColor [UIColor colorWithRGB:0xa5a5a5]
#import "TTSelectLabel.h"

#import "CourseCommentViewController.h"
#import <IQKeyboardManager.h>

@interface CourseCommentViewController ()
@property (nonatomic,strong) EDStarRating * ratingView;
@property (nonatomic,strong) UIImageView *avrater;
@property (nonatomic,strong) TTScrollView *scrollView;
@property (nonatomic,strong) UITextView *textView;
@end

@implementation CourseCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    self.showNavigationBar = YES;
    
    [self setControllerTitle:@"我的评价"];
    UIBarButtonItem * backItem = [UIBarButtonItem itemWithImage:@"back_icon" selectImage:@"" addTarget:self andAction:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = backItem;
    
    [self creatSubviews];
    // Do any additional setup after loading the view from its nib.
}

- (void)creatSubviews
{
    MFWUserInfo * userInfo = [MFWSdkEnvironment sharedInstance].userInfo;
    _scrollView = [TTScrollView new];
    [self.view addSubview:_scrollView];
    _scrollView.frame = CGRectMake(0, 0, kScreenWidth, self.view.height - 60);
    
    _avrater = [UIImageView new];
    _avrater.frame = CGRectMake(27, 27, 60, 60);
    [_scrollView addSubview:_avrater];
    [_avrater sd_setImageWithURL:[NSURL URLWithString:kAPI_IMAGE_HOST(userInfo.logoUrl)] randomPlaceHolder:YES];
    
    _ratingView = [EDStarRating new];
    _ratingView.backgroundColor = [UIColor whiteColor];
    _ratingView.starImage = [UIImage imageNamed:@"course_star_comment"];
    _ratingView.starHighlightedImage = [UIImage imageNamed:@"course_star_comment_highlighted"];
    _ratingView.maxRating = 5.0;
    _ratingView.horizontalMargin = 0;
    _ratingView.editable = YES;
    _ratingView.displayMode = EDStarRatingDisplayFull;
    _ratingView.rating = 5;
    [_scrollView addSubview:_ratingView];
    [_ratingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_avrater.mas_right).offset(16);
        make.top.mas_equalTo(_avrater.mas_top);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(20);
    }];
    
    UILabel * label = [UILabel new];
    label.text = @"请对本课时发表下评价吧......";
    label.fontSize = 16;
    label.textColor = kDark_TextColor_Default;
    [_scrollView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_ratingView.mas_left);
        make.bottom.mas_equalTo(_avrater.mas_bottom);
    }];
    
    UIView * lineView = [UIView new];
    lineView.backgroundColor = lineColor;
    lineView.size = CGSizeMake(_scrollView.width - leftRightMargin * 2, 1);
    lineView.left = leftRightMargin;
    lineView.top = _avrater.bottom + 50;
    [_scrollView addSubview:lineView];
   
    NSString * contentString = @"对老师的印象";
    UIFont * font = [UIFont systemFontOfSize:15];
    NSInteger width = ceil([contentString widthForFont:font]) + 28;
    
    
    UILabel * label1 = [UILabel new];
    label1.backgroundColor = [UIColor whiteColor];
    label1.textColor = kDark_TextColor_Default;
    label1.fontSize = 15;
    label1.text = contentString;
    label1.textAlignment = NSTextAlignmentCenter;
    label1.size = CGSizeMake(width, 30);
    label1.centerX = lineView.centerX;
    label1.centerY = lineView.centerY;
    [_scrollView addSubview:label1];
    
    CGFloat lineHeight = 30;
    CGFloat lineSpacen = 13;
    NSInteger count = _commentList.count;
    for (int i = 0; i < _commentList.count; i ++){
        CourseCommentQuickCommentModel * model = _commentList[i];
        TTSelectLabel * label = [[TTSelectLabel alloc]initWithTitle:model.quickCommentContent];
        label.commentId = model.commentId;
        label.left = i%2 == 0 ? leftRightMargin : leftRightMargin + label.width + 16;
        label.top =  label1.bottom + ((int)i/2 * (lineHeight + lineSpacen)) + 18;
        [_scrollView addSubview:label];
    }
    CGFloat secondStartY = label1.bottom + ((int)count/2 * (lineHeight + lineSpacen)) + 36 + lineHeight;
    
    CALayer * lineLayer = [CALayer new];
    lineLayer.backgroundColor = lineColor.CGColor;
    lineLayer.size = CGSizeMake(_scrollView.width - leftRightMargin * 2, 1);
    lineLayer.left = leftRightMargin;
    lineLayer.top = secondStartY + 20;
    [_scrollView.layer addSublayer:lineLayer];
    
    UILabel * label2 = [UILabel new];
    label2.backgroundColor = [UIColor whiteColor];
    label2.textColor = kDark_TextColor_Default;
    label2.fontSize = 15;
    label2.text = @"对课时的评价";
    label2.textAlignment = NSTextAlignmentCenter;
    label2.size = CGSizeMake(width, 30);
    label2.centerX = lineLayer.centerX;
    label2.centerY = lineLayer.centerY;
    [_scrollView addSubview:label2];
    
    _textView = [[UITextView alloc]initWithFrame:CGRectMake(leftRightMargin, label2.bottom + 10, lineLayer.width, 115)];
    _textView.layer.borderWidth = 1;
    _textView.font = [UIFont systemFontOfSize:15];
    _textView.layer.borderColor = [UIColor colorWithARGB:0xcccccc].CGColor;
    _textView.backgroundColor = [UIColor colorWithARGB:0xf5f5f5];
    [_scrollView addSubview:_textView];
    
    _scrollView.contentSize = CGSizeMake(_scrollView.width, _textView.bottom + 10);
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)commentAction:(id)sender {
    /*
     user_id 用户id
     source_id 课程id
     source_type 0写死，代表课程
     appraise_content 内容
     score 分数
     reason 评价项
     id多个以逗号隔开 例: “1，2，3”
     */
    NSString * commentString = _textView.text;
    if (!commentString || commentString.length == 0){
        [self.view showTip:@"请输入评论内容~"];
        return;
    }
    NSString * userId = [MFWSdkEnvironment sharedInstance].userInfo.uid;
    NSString * score = [NSString stringWithFormat:@"%d",(int)_ratingView.rating];
    NSMutableArray * commentIds = @[].mutableCopy;
    for (id subObj in _scrollView.subviews) {
        if ([subObj isKindOfClass:[TTSelectLabel class]]){
            TTSelectLabel * label = subObj;
            if (label.state){
                [commentIds addObject:label.commentId];
            }
        }
    }
    NSString * comment = [commentIds componentsJoinedByString:@","];
    
    NSMutableDictionary * parms = @{@"user_id":userId,
                                    @"source_id":_courseId,
                                    @"source_type":@"0",
                                    @"appraise_content":_textView.text,
                                    @"score":score,
                                    @"reason":comment
                                    }.mutableCopy;
//    if (comment.length > 0){
//        [parms setObject:comment forKey:@"reason"];
//    }
    
    [SVProgressHUD showWithStatus:@"评论发送中..."];
    [BaseHttpTool get:kAPI_COURSE_COMMETN_ADD params:parms success:^(id responseObj) {
        [SVProgressHUD showSuccessWithStatus:@"评论成功~"];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
